<?php
class Ab_Image extends Ab_File{
	private $path=null;
	private $imageType=null;
	
	function __construct($path){
		parent::__construct($path);
		
		$this->path=$path;
		
		if(!@getimagesize($this->path)){
			throw new Exception('The path '.$this->path.' is not a valid image.');
		}		
	}
	
	public function getPath(){
		return $this->path;
	}
	
	
	public function getImageType(){
		$imageFactory=new Ab_ImageFactory($this->path);
		$this->imageType=$imageFactory->getImageType();
		return $this->imageType;
	}
	
	public function getType(){
		return 'image';
	}
	
	public function remove(){
		$extension=$this->getExtension();
		$preview_path=PREVIEW_DIR.'/'.md5($this->path).$extension;
		$thumb_path=CACHE_DIR.'/'.md5($this->path).$extension;	
		
		if(is_file($preview_path)){
			unlink($preview_path);
		}
		
		if(is_file($thumb_path)){
			unlink($thumb_path);
		}
	
		return unlink($this->path);
	}
	
	public function getPreview(){
		$extension=$this->getExtension();
		$thumb_path=PREVIEW_DIR.'/'.md5($this->path).$extension;	
		
		if(!is_file($thumb_path)){
			return $this->resizeImage(PREVIEW_DIR,135,100);
		}
		elseif(filemtime($thumb_path)>filemtime($this->path)){
			return $this->resizeImage(PREVIEW_DIR,135,100);
		}
		
		return $thumb_path;
	}
	
	public function getThumb(){
		$extension=$this->getExtension();
		$thumb_path=CACHE_DIR.'/'.md5($this->path).$extension;
		
		
		if(!is_file($thumb_path)){
			return $this->resizeImage(CACHE_DIR);
		}
		elseif(filemtime($thumb_path)>filemtime($this->path)){
			return $this->resizeImage(CACHE_DIR);
		}
		
		return $thumb_path;
	}
	
	public function resizeImage($dir_path,$width=100,$height=80){
		if(!$this->imageType){
			$this->getImageType();
		}
		
		list($width_orig, $height_orig, $type, $attr) = getimagesize($this->path);

		// Get new dimensions
		$dimensions=$this->getResizeDimensions($width_orig, $height_orig, $width, $height);
		$height=$dimensions['height'];
		$width=$dimensions['width'];
		
		return $this->imageType->makeThumb($dir_path,$width,$height,$width_orig, $height_orig);
	}
	
	private function getResizeDimensions($original_w, $original_h, $thumb_w, $thumb_h){
		$dimensions=array();

		if ($original_w > $thumb_w || $original_h > $thumb_h)
		{
			do
			{
				$okay = true;
				if ($original_w > $thumb_w)
				{
					$new_size = $this->resize_fixed_width($thumb_w, $original_w, $original_h);
					$original_w = $new_size['w'];
					$original_h = $new_size['h'];
					$okay = false;
				}
				elseif ($original_h > $thumb_h)
				{
					$new_size = $this->resize_fixed_height($thumb_h, $original_w, $original_h);
					$original_w = $new_size['w'];
					$original_h = $new_size['h'];
					$okay = false;
				}
			}
			while (!$okay);
		}

		$dimensions['height']=$original_h;
		$dimensions['width']=$original_w;

		return $dimensions;
	}
	
	private function resize_fixed_height($height, $o_w, $o_h){
		$ret = array();

		$ret['h'] = $height;
		$ret['w'] = floor($height * $o_w / $o_h);

		return $ret;
	}

	private function resize_fixed_width($width, $o_w, $o_h){
		$ret = array();

		$ret['h'] = floor($width * $o_h / $o_w);
		$ret['w'] = $width;

		return $ret;
	}
	
}
?>
